package com.ruoyi.web.controller.client;

import com.ruoyi.base.domain.client.vx.WxPrepayWithRequestPaymentResponse;
import com.ruoyi.base.domain.client.vx.WxRequestBody;
import com.ruoyi.base.service.IWechatPayService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.ResponseEntity;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;

/**
 * @ClassName WechatPayController
 * @Description TODO 微信支付接口
 * @Author Mr、Liu
 * @Date 2025/6/25 22:46
 * @Version 1.0
 */

@Api(tags = "微信支付接口")
@RestController
@RequestMapping("/client/pay")
public class WechatPayController {

    @Resource
    private IWechatPayService payService;


    @ApiOperation(value = "原生的统一下单接口")
    @PostMapping("/unifiedOrder")
    public ResponseEntity unifiedOrder(@RequestBody WxRequestBody requestBody) throws Exception {

        String tradeNo = Optional.ofNullable(requestBody).map(WxRequestBody::getTradeNo).get();
        if (StringUtils.isBlank(tradeNo)) {
            return new ResponseEntity().error("参数异常");
        }

        WxPrepayWithRequestPaymentResponse wxPrepayWithRequestPaymentResponse = payService.unifiedOrder(requestBody);

        return new ResponseEntity().success(wxPrepayWithRequestPaymentResponse);
    }


    @ApiOperation(value = "支付成功回调接口", hidden = true)
    @PostMapping("/payNotify")
    @Anonymous
    public Map<String,Object> payNotify(HttpServletRequest request)
    {
        return payService.payNotify(request);
    }
}
